package com.rxb.blog.service.impl;

import com.rxb.blog.entity.ArticleTypeEntity;
import com.rxb.blog.entity.FileEntity;
import com.rxb.blog.mapper.ArticlePictureMapper;
import com.rxb.blog.mapper.ArticleTypeMapper;
import com.rxb.blog.mapper.DictionaryMapper;
import com.rxb.blog.service.ArticleTypeService;
import com.rxb.blog.utils.BlogException;
import com.rxb.blog.utils.FileUtil;
import com.rxb.blog.vo.ArticleTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-02-28 17:18
 * Description：<描述>
 */
@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Autowired
    private ArticlePictureMapper articlePictureMapper;

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Value("${blog.picUrl}")
    private String picUrl;

    @Override
    public Map getArticleTypes( Map<String,Object> map) {
        int startNum = 0;
        startNum =  ((int)(map.get("currentPage")) -1) * (int)map.get("pageSize");
        map.put("startNum",startNum);

        Map map1 = new HashMap();
        List<ArticleTypeEntity> articleTypes = articleTypeMapper.getArticleTypes(map);
        int articleTotal = articleTypeMapper.getArticleTotal(map);
        map1.put("articleTypes",articleTypes);
        map1.put("articleToptal",articleTotal);
        return map1;
    }

    @Override
    public List<Map> getArticleTypesForHome() {
        List<Map> list = articleTypeMapper.getArticleTypesForHome();
        return list;
    }

    @Override
    public void insertArticleType(ArticleTypeVo articleTypeVo) throws BlogException{
        //String article_type_logo_path = dictionaryMapper.getValueByKey("article_type_logo_path");

        //根据作者&项目类别，查询当前作者创建的项目类别（项目类别不能一样）
        int articleTypeEntity =  articleTypeMapper.getArticleTypeByUsernameAndType(articleTypeVo);
        if(articleTypeEntity != 0){
            throw new BlogException("当前类型已经存在");
        }

        //文章类型 新增
        articleTypeMapper.insertArticleType(articleTypeVo);

        if(articleTypeVo.getFileName() != null && articleTypeVo.getFileName().length() > 0){ //判断   是否上传了文件
            //文章类型 图片 新增

            String username = articleTypeVo.getUsername();
            FileEntity fileEntity = new FileEntity();
            fileEntity.setCreate_by(username);
            fileEntity.setUpdate_by(username);
            fileEntity.setName(articleTypeVo.getFileName());
            fileEntity.setArticle_type_id(articleTypeVo.getId());

            //文件后缀
            String suffix =  articleTypeVo.getFileName().substring(articleTypeVo.getFileName().lastIndexOf("."));

            fileEntity.setPicture_url(picUrl+ File.separator +articleTypeVo.getPicUUID() + suffix);
            fileEntity.setUsername(username);

            articlePictureMapper.insertFile(fileEntity);
        }

    }

    @Override
    public ArticleTypeEntity getArticleTypeById(int id) {
        ArticleTypeEntity article = articleTypeMapper.getArticleTypeById(id);
        return article;
    }

    @Override
    public void updateArticleType(ArticleTypeVo articleTypeVo)  throws BlogException{
        //String article_type_logo_path = dictionaryMapper.getValueByKey("article_type_logo_path");
        //根据作者&项目类别，查询当前作者创建的项目类别（项目类别不能一样）
        int articleTypeEntity =  articleTypeMapper.getArticleTypeByUsernameAndType(articleTypeVo);
        if(articleTypeEntity != 0){
            throw new BlogException("当前类型已经存在");
        }
        articleTypeMapper.updateArticleType(articleTypeVo);


        if(articleTypeVo.getFileName() != null && articleTypeVo.getFileName().length() > 0){ //判断   是否上传了文件
            //文章类型 图片 新增

            String username = articleTypeVo.getUsername();
            FileEntity fileEntity = new FileEntity();
            fileEntity.setUpdate_by(username);
            fileEntity.setName(articleTypeVo.getFileName());
            fileEntity.setArticle_type_id(articleTypeVo.getId());

            //文件后缀
            String suffix =  articleTypeVo.getFileName().substring(articleTypeVo.getFileName().lastIndexOf("."));

            fileEntity.setPicture_url(picUrl+ File.separator +articleTypeVo.getPicUUID() + suffix);
            fileEntity.setUsername(username);

            if(null == articleTypeVo.getPicture_id()){ //原先没有上传图片
                fileEntity.setCreate_by(username);
                articlePictureMapper.insertFile(fileEntity);
            }else{
                //删除文件的时候，已经把文件信息删除，下面不需要
                //fileEntity.setPicture_id(articleTypeVo.getPicture_id());
                //articlePictureMapper.updateFile(fileEntity);
            }
        }
    }

    @Override
    public void deleteArticleType(Map<String,Object> map) {
        articleTypeMapper.deleteArticleType((Integer) map.get("id"));
        if(null != map.get("picture_id")){
            articlePictureMapper.deleteArticlePicture((Integer) map.get("picture_id"));
            FileUtil.deleteFile(map.get("picture_url").toString());
        }
    }

    @Override
    public List<ArticleTypeEntity> getArticleTypesAll(Map<String,Object> map) {
        List<ArticleTypeEntity> articleTypesAll = articleTypeMapper.getArticleTypesAll(map);
        articleTypesAll.forEach(a ->{
            a.setFlag(false);
        });
        return articleTypesAll;
    }


}
